package edu.bluebeanforum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

//  统一返回结果
public class AppResult <T>{
    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)  //不论什么时候都参与序列化    为null默认不参与序列化
    //序列化是将对象的状态信息转换为可以存储或传输的形式的过程。在这个过程中，对象的各种属性值会被按照一定的规则转换为字节序列，以便能够在不同的环境中进行保存、传输或重建对象
    private int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    //具体的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;
    public AppResult(int code, String message) {
        this(code,message,null);
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    /*
    * 成功时返回的方法    定义为static是为了方便大家都可以调用
    * */
    public static AppResult success(){
        return  new AppResult(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    public static <T>AppResult<T> success(T data){
        return  new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage(),data);
    }
    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T>AppResult<T> success(String message,T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message,data);
    }

    /*
    *
    * 失败时返回的方法
    * */

    public static  AppResult fail(){
        return new AppResult(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }

    public static AppResult fail(String  message){
        return  new AppResult(ResultCode.FAILED.getCode(),message);
    }

    public static AppResult fail(ResultCode resultCode){
        return  new AppResult(resultCode.getCode(),resultCode.getMessage());
    }



    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
